package javax.per.toolkit.parse;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;

import tools.Env;

public class JavaParser {
	private ASTParser astParser = ASTParser.newParser(AST.JLS3);
	
	public static void main(String[] args){
		JavaParser jp=new JavaParser();
		try {
			CompilationUnit unit=jp.getCompilationUnit(Env.get("desktop")+File.separator+"aaa.java");
			unit.getClass();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public CompilationUnit getCompilationUnit(String javaFilePath)
			throws Exception {

		BufferedInputStream bufferedInputStream = new BufferedInputStream(
				new FileInputStream(javaFilePath));
		byte[] input = new byte[bufferedInputStream.available()];
		bufferedInputStream.read(input);
		bufferedInputStream.close();
		this.astParser.setSource(new String(input).toCharArray());
		CompilationUnit result = (CompilationUnit) (this.astParser
				.createAST(null));
		return result;
	}

}
